package com.geek.factory.controller;

import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.dto.ProductionScheduleDTO;
import com.geek.factory.entity.ProductionSchedule;
import com.geek.factory.result.Result;
import com.geek.factory.service.ProductionScheduleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/schedule")
public class ProductionScheduleController {

    @Autowired
    private ProductionScheduleService productionScheduleService;

    // ✅ 查询列表（含产线名称和计划编码）
    @PostMapping("/list")
    public Result list(@RequestBody ProductionScheduleDTO dto) {
        List<ProductionScheduleDTO> list = productionScheduleService.getScheduleWithNames(dto);
        return new Result(SuccessConstant.SUCCESS, "查询成功", list);
    }

    // ✅ 根据 ID 查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        ProductionSchedule schedule = productionScheduleService.getById(id);
        if (schedule != null) {
            return new Result(SuccessConstant.SUCCESS, "查询成功", schedule);
        } else {
            return new Result(SuccessConstant.FAIL, "排班不存在");
        }
    }

    // ✅ 新增
    @PostMapping("/add")
    public Result add(@RequestBody ProductionSchedule schedule) {
        // 根据 production_plan_id 查询 plan_code
        String planCode = productionScheduleService.getPlanCodeById(schedule.getProductionPlanId());
        if (planCode == null) {
            return new Result(SuccessConstant.FAIL, "对应的生产计划不存在");
        }

        // 根据 product_line_id 查询 line_name
        String lineName = productionScheduleService.getProductLineNameById(String.valueOf(schedule.getProductLineId()));
        if (lineName == null) {
            return new Result(SuccessConstant.FAIL, "对应的产线不存在");
        }

        // 补充字段
        schedule.setProductionPlanCode(planCode);
        schedule.setLineName(lineName);

        // 保存排班记录
        boolean success = productionScheduleService.save(schedule);
        if (success) {
            return new Result(SuccessConstant.SUCCESS, "新增成功");
        } else {
            return new Result(SuccessConstant.FAIL, "新增失败");
        }
    }

    // ✅ 修改
    @PutMapping("/update")
    public Result update(@RequestBody ProductionSchedule schedule) {
        boolean success = productionScheduleService.updateById(schedule);
        if (success) {
            return new Result(SuccessConstant.SUCCESS, "更新成功");
        } else {
            return new Result(SuccessConstant.FAIL, "更新失败");
        }
    }

    // ✅ 删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean success = productionScheduleService.removeById(id);
        if (success) {
            return new Result(SuccessConstant.SUCCESS, "删除成功");
        } else {
            return new Result(SuccessConstant.FAIL, "删除失败");
        }
    }

    @GetMapping("/shift/{shift}")
    public Result getByShift(@PathVariable String shift) {
        List<ProductionScheduleDTO> list = productionScheduleService.getByShift(shift);
        return new Result(SuccessConstant.SUCCESS, "查询成功", list);
    }


}
